//var dao = require("./mysql.js");
//var map = require("../util/map.js");
/*var express = require("express");
 var app = express();
 var server = require("http").createServer(app);
 var io = require('socket.io').listen(server);*/

var express = require("express");
var app = express()
var server = require('http').Server(app);
var io = require("socket.io")(server);
var users = new Array();

/**
 * express 路径跳转
 */

app.use(express.static(__dirname + '/public/'));

app.get("/",function(req,res){
    //若访问的内容在执行文件之外会照成文件禁止访问
    console.log(__dirname);
    res.sendfile(__dirname + "/html/login.html");
});

app.get("/dologin",function(req,res){
    console.log("登陆页面");
    res.sendfile(__dirname + "/html/chatIndex.html");
});

server.listen(8800,function(){
    console.log("端口打开成功");
});

io.sockets.on("connection",function(socket){
    console.log("----用户登陆----");

    socket.on("login",function(name){
        console.log(name);
        var temp = name;
        if(undefined!=temp && null!=temp ){
            var obj = {};
            obj.soc = socket;
            obj.id = name;
            users.push(obj);

            console.log(users.length);
            socket.emit("islogin",true);
        }

    });

    /**
     * 聊天内容
     */
    socket.on("message",function(data){
        console.log("聊天---"+data.getUser+"----"+users.length);
        var mysocket = null;

        for(var i=0;i<users.length;i++ ) {
            console.log(users[i].id);
            if(users[i].id == data.getUser){
                mysocket = users[i].soc;
                console.log("-----------"+i);
            }
        }

        /**
         * @TODO
         * 这里只标记了存在的情况，并没有写不存在的情况
         */
        if(mysocket !=null ){
            mysocket.emit("getmsg",data);
        }


    });

    socket.on("disconnected",function(user){
        console.log("用户退出");
//        map.remove(user);
    });



})